/*
 * FileImporter.java
 *
 *  created: 29.9.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.core;

import cz.mp.k3bg.log.LoggerManager;
import cz.mp.util.FileUtils;
import cz.mp.util.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * {@code FileImporter} je základ pro kopírování souborů (a modifikace 
 * těchto souborů) do zadaného adresáře.
 * 
 * @author Martin Pokorný
 * @version 0.1
 * @see HtmlFilesImporter
 */
public abstract class FileImporter {

    private static final boolean DEBUG = false;
    private static final Logger logger =
            LoggerManager.getLogger(FileImporter.class, DEBUG);

    protected String destDir = "";    
    
    /**
     * 
     * @param destDir 
     */
    public FileImporter(String destDir) {
        setDestDirImpl(destDir);
    }

    /**
     * 
     * @param destDir 
     */
    private void setDestDirImpl(String destDir) {
        if (StringUtils.isBlank(destDir)) {
            throw new IllegalArgumentException("destDir is blank");
        }
        logger.finer("destDir = " + destDir);
        File dir = new File(destDir);
        if (! dir.exists()) {
            dir.mkdirs();
        }
        if (! dir.isDirectory()) {
            throw new IllegalArgumentException("destDir isn't dir");
        }
        this.destDir = destDir;
    }

    /**
     * 
     * @return 
     */
    public String getDestDir() {
        return destDir;
    }
    
    /**
     * 
     * @param item
     * @param baseDirPath
     * @return 
     */
    protected String createCopyFilePathInNewDir(Item item, String baseDirPath) {
        String result = FileUtils.getCopyFilePathInNewDir(
                item.getPath(), baseDirPath, destDir);
        FileUtils.createDirs(result);
        return result;
    }    
    
    public abstract void importt() throws IOException;
            
}   // FileImporter.java

